package com.jumi.microservice.controller.api;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.jm.member.common.jwt.annon.IgnoreUrlsAnnon;
import com.jm.member.common.messages.enmus.WeChartLanguageEnum;
import com.jm.member.common.messages.entity.JiGuangResponse;
import com.jm.member.common.messages.entity.WeChartResponse;
import com.jm.member.common.messages.entity.WeChartUserInfo;
import com.jm.member.common.messages.util.JiGuangUtil;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.dto.reponse.api.LoginResponse;
import com.jumi.microservice.entity.JmUser;
import com.jumi.microservice.enums.CommonEnum;
import com.jumi.microservice.enums.MessageType;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/H5Login")
@Api(tags = "C-H5登录接口")
public class H5LoginController extends LoginController{


    @IgnoreUrlsAnnon("/weChartH5")
    @PostMapping("/weChartH5")
    @ApiOperation("微信H5获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inviterId",value = "邀请人id"),
            @ApiImplicitParam(name = "registerId",value = "registerId")
    })
    public ResponseResult weChartH5(@ApiParam(required = true, value = "code") @RequestParam String code,
                                    String inviterId, String registerId) {
        Long memberInviterId =  (inviterId != null && inviterId.length() != 0) ?  Long.parseLong(inviterId) : 0;
        WeChartResponse accessToken = weChartService.getAccessToken(code);
        String access_token = accessToken.getAccess_token();
        String openid = accessToken.getOpenid();
        if (StrUtil.isNotEmpty(access_token) && StrUtil.isNotEmpty(openid)) {
            WeChartUserInfo weChartUserInfo = weChartService.getWeChartUserInfo(access_token, openid, WeChartLanguageEnum.简体);
            String unionid = weChartUserInfo.getUnionid();
            if (StrUtil.isNotEmpty(unionid)) {
                JmUser jmUser = checkExistByUnionId(unionid);
                if (ObjectUtil.isEmpty(jmUser)) {
                    return ResponseResult.success("用户未绑定", weChartUserInfo);
                }
                LoginResponse response = login(jmUser,memberInviterId,registerId);
                return ResponseResult.success(response);
            }
            return ResponseResult.success(weChartUserInfo);
        }
        return ResponseResult.error(500,"微信获取失败", accessToken);
    }

    @PostMapping("/loginWeChart")
    @IgnoreUrlsAnnon("/loginWeChart")
    @ApiOperation(value = "微信登录--继承接口",response = LoginResponse.class)
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "token",value = "运营商下发的token"),
                    @ApiImplicitParam(name = "code",value = "验证码"),
                    @ApiImplicitParam(name = "inviterId",value = "邀请人id"),
                    @ApiImplicitParam(name = "registerId",value = "registerId")
            }
    )
    public ResponseResult loginWeChart(@Validated WeChartUserInfo weChartUserInfo,
                                       @ApiParam(required = true, value = "手机号") @RequestParam String mobile,
                                       String token, String code, String inviterId, String registerId){
        Long memberInviterId =  (inviterId != null && inviterId.length() != 0) ?  Long.parseLong(inviterId) : 0;
        if(ObjectUtil.isAllNotEmpty(token,code)||ObjectUtil.isAllEmpty(token,code)){
            return ResponseResult.error("选择正确的认证方式!");
        }else if(ObjectUtil.isNotEmpty(code)){
            //验证码
            CheckSms(mobile, code, MessageType.登录验证);
        }else if(ObjectUtil.isNotEmpty(token)){
            JiGuangResponse response = JiGuangUtil.verify(token, mobile);
            if (!response.getCode().equals(JiGuangResponse.successVerifyCode)) {
                return ResponseResult.error("验证手机号失败!");
            }
        }
        ResponseResult rs = loginWeChartBinding(weChartUserInfo, mobile,memberInviterId,registerId);
        return rs;
    }

    @PostMapping("/checkWeChatBound")
    @IgnoreUrlsAnnon("/checkWeChatBound")
    @ApiOperation("验证微信是否绑定用户")
    public ResponseResult checkExist(@ApiParam(required = true, value = "微信unionId")  @RequestParam String unionId){
        JmUser jmUser = checkExistByUnionId(unionId);
        int returnVal=0;
        if (!ObjectUtil.isEmpty(jmUser)) {
            returnVal= CommonEnum.是.getValue();
        }
        return ResponseResult.success(returnVal);
    }

}
